package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/22  17:21
 */
@Service
public class AddCartServiceImpl implements ICartService {

    String secondKey;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    CartItemConverter cartItemConverter;

    @Override
    public CartListByIdResponse getCartListById(CartListByIdRequest request) {

        CartListByIdResponse response = new CartListByIdResponse();
        try {
            request.requestCheck();
            RMap<Object, CartProductDto> shopCart = redissonClient.getMap(""+request.getUserId());
            if(shopCart==null){
                response.setCode(ShoppingRetCode.SUCCESS.getCode());
                response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
                return response;
            }else {
                List<CartProductDto> cartProductDtos = new ArrayList<>();
                Collection<CartProductDto> values = shopCart.values();
                for (CartProductDto value : values) {
                    cartProductDtos.add(value);
                }


                response.setCode(ShoppingRetCode.SUCCESS.getCode());
                response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
                response.setCartProductDtos(cartProductDtos);


            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }

        return response;
    }

    @Override
    public AddCartResponse addToCart(AddCartRequest request) {
        CartProductDto cartProductDto = new CartProductDto();
        AddCartResponse addCartResponse = new AddCartResponse();

        request.requestCheck();
        try {
            Item item = itemMapper.selectByPrimaryKey(request.getProductId());
            BigDecimal price = item.getPrice();
            Long limitNum = Long.valueOf(item.getLimitNum());
            String title = item.getTitle();

            String[] images = item.getImages();
            for (String s : images) {
                cartProductDto.setProductImg(s);
            }


            cartProductDto.setProductId(request.getProductId());
            cartProductDto.setChecked("");
            cartProductDto.setSalePrice(price);
            cartProductDto.setLimitNum(limitNum);
            cartProductDto.setProductName(title);

            cartProductDto.setProductNum(request.getProductNum());

            RMap<String, CartProductDto> map = redissonClient.getMap(""+request.getUserId());

                //获取已有的productId
              //  Long shopCartPid = map.get(""+request.getProductId()).getProductId();
            CartProductDto cartProductDto1 = map.get("" + request.getProductId());



                //如果已经存在productId则只是增加数量
                 if(cartProductDto1!=null){
                     CartProductDto allData = map.get(""+request.getProductId());
                     Integer productNum = allData.getProductNum();
                     allData.setProductNum(productNum+1);
                     //如果没有则新增一条数据
                 }else {
                     map.put(""+request.getProductId(),cartProductDto);
                 }


            addCartResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            addCartResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            addCartResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            addCartResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }


        return addCartResponse;
    }

    @Override
    public UpdateCartNumResponse updateCartNum(UpdateCartNumRequest request) {
        UpdateCartNumResponse updateCartNumResponse = new UpdateCartNumResponse();
        request.requestCheck();
        try {
            RMap<Object, CartProductDto> shopCart = redissonClient.getMap(""+request.getUserId());
            CartProductDto allData = shopCart.get(""+request.getItemId());
            allData.setProductNum(request.getNum());
            allData.setChecked(request.getChecked());
            shopCart.put(""+request.getItemId(),allData);

            updateCartNumResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            updateCartNumResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            updateCartNumResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            updateCartNumResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }


        return updateCartNumResponse;
    }

    @Override
    public CheckAllItemResponse checkAllCartItem(CheckAllItemRequest request) {

        CheckAllItemResponse checkAllItemResponse = new CheckAllItemResponse();
        request.requestCheck();
        try {
            RMap<String, CartProductDto> map = redissonClient.getMap("" + request.getUserId());
            for (String result : map.keySet()) {
                CartProductDto cartProductDto = map.get(result);
                cartProductDto.setChecked(request.getChecked());
            }
            checkAllItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            checkAllItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            checkAllItemResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            checkAllItemResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }

        return checkAllItemResponse;
    }

    @Override
    public DeleteCartItemResponse deleteCartItem(DeleteCartItemRequest request) {
        DeleteCartItemResponse deleteCartItemResponse = new DeleteCartItemResponse();
        request.requestCheck();
        try {
            RMap<Object, CartProductDto> shopCart = redissonClient.getMap(""+request.getUserId());
            CartProductDto allData = shopCart.get(""+request.getItemId());
            if(allData.getProductId().equals(request.getItemId())){
                shopCart.remove(""+request.getItemId());

            }
            deleteCartItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            deleteCartItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            deleteCartItemResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            deleteCartItemResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }


        return deleteCartItemResponse;
    }

    @Override
    public DeleteCheckedItemResposne deleteCheckedItem(DeleteCheckedItemRequest request) {
        DeleteCheckedItemResposne deleteCheckedItemResposne = new DeleteCheckedItemResposne();
        try {
            request.requestCheck();
            RMap<String, CartProductDto> map = redissonClient.getMap("" + request.getUserId());

            for (String item : map.keySet()) {
                CartProductDto cartProductDto = map.get(item);
                if(Boolean.parseBoolean(cartProductDto.getChecked())==true){
                    map.remove(item);

                }
            }
            deleteCheckedItemResposne.setCode(ShoppingRetCode.SUCCESS.getCode());
            deleteCheckedItemResposne.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            deleteCheckedItemResposne.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            deleteCheckedItemResposne.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }


        return deleteCheckedItemResposne;
    }
    //实现暴露接口
    @Override
    public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
        ClearCartItemResponse clearCartItemResponse = new ClearCartItemResponse();

        request.requestCheck();

        try {
            RMap<String, CartProductDto> map = redissonClient.getMap("" + request.getUserId());
            for (String result : map.keySet()) {
                CartProductDto cartProductDto = map.get(result);
                if(Boolean.parseBoolean(cartProductDto.getChecked())==true){
                    map.remove(result);
                }
            }
            clearCartItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            clearCartItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            clearCartItemResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            clearCartItemResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
        }


        return clearCartItemResponse;
    }


}
